package com.zxx.ssm.controller;

import com.zxx.ssm.dao.Admin;
import com.zxx.ssm.dao.AdminExample;
import com.zxx.ssm.model.enums.ResultCode;
import com.zxx.ssm.model.result.BizResponse;
import com.zxx.ssm.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("admin")
@Controller
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public BizResponse login(String code, String password, HttpServletRequest request) {
        AdminExample adminExample = new AdminExample();
        AdminExample.Criteria criteria = adminExample.createCriteria();
        criteria.andAdminnameEqualTo(code);
        criteria.andPasswdEqualTo(password);
        List<Admin> adminList = adminService.selectByExample(adminExample);
        if (adminList.size() > 0) {
            HttpSession session = request.getSession();
            session.setAttribute("user", adminList.get(0));
            return new BizResponse();
        } else
            return new BizResponse(ResultCode.ADMIN_LOGIN_ERROR);
    }

    @RequestMapping(value = "logout", method = RequestMethod.GET)
    @ResponseBody
    public BizResponse logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("user", null);
        return new BizResponse();
    }
}
